﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using VIB.Model;

namespace VIB.View
{
    /// <summary>
    /// Interaction logic for UserManagement.xaml
    /// </summary>
    public partial class UserManagement : Window
    {
        public UserManagement()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshControlStatus(true);
        }

        public void RefreshControlStatus(bool refreshDataGrid)
        {
            if (dataGrid_users.SelectedItems.Count > 0)
            {
                button_edit.IsEnabled = true;
                button_delete.IsEnabled = true;
            }
            else
            {
                button_edit.IsEnabled = false;
                button_delete.IsEnabled = false;
            }
            if (refreshDataGrid)
            {
                try
                {
                    dataGrid_users.ItemsSource = DataManager.GetUsers().DefaultView;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
               
                dataGrid_users.SelectedItem = null;
            }            
        }

        private void AddUser(object sender, RoutedEventArgs e)
        {
            EditUser newUser = new EditUser();
            newUser.ShowDialog();
            RefreshControlStatus(true);
        }

        private void DeleteUser(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid_users.SelectedItem;

            int idUser = (int)row["iduser"];

            if (MessageBox.Show((string)row["firstname"] + " " + (string)row["lastname"] + "\nwirklich aus der Datenbank entfernen?", "Wirklich löschen?",
                MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                try
                {
                    if (DataManager.HasActiveOwnerships(idUser))
                    {
                        MessageBox.Show("Benutzer kann nicht gelöscht werden, während er noch Bücher ausgeliehen hat");
                    }
                    else
                    {
                        DataManager.DeleteUser(idUser);
                    }                
                }               
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
               
            }
            RefreshControlStatus(true);
        }

        private void dataGrid_users_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RefreshControlStatus(false);
        }

        private void EditUser(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)dataGrid_users.SelectedItem;
            

            int idUser = (int)row["iduser"];
            string firstName = (string)row["firstname"];
            string lastName = (string)row["lastName"];
            string email = (string)row["email"];

            EditUser edit = new EditUser(new User(idUser, firstName, lastName, email));
            edit.ShowDialog();
            RefreshControlStatus(true);
        }
    }
}
